﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class SafeInventoryRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15271;
        protected GridView gvSafeInventory;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalAvailable;
        protected Label lblTotalAvailableQty;
        protected Label lblTotalMax;
        protected Label lblTotalMaxQty;
        protected Label lblTotalMin;
        protected Label lblTotalMinQty;
        protected Label lblTotalPreallocated;
        protected Label lblTotalPreallocatedQty;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected Label lblTotalReorderPoint;
        protected Label lblTotalReorderPointText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalAvailableQty;
        private decimal TotalInvQtyText;
        private decimal TotalMaxQty;
        private decimal TotalMinQty;
        private decimal TotalPreallocatedQty;
        private decimal TotalReorderPoint;
        protected UpdatePanel up1;

        private void BindData2SafeInventoryList(SafeInventoryRptQueryEntity safeInventoryRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(safeInventoryRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetSafeInventoryRptInfo(safeInventoryRptQuery);
            base.BindData2GridView(this.gvSafeInventory, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            safeInventoryRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = safeInventoryRptQuery.CurrentPage;
            safeInventoryRptQuery.CurrentPage = "0";
            this.gvSafeInventory.Columns[1].Visible = false;
            this.gvSafeInventory.AllowSorting = false;
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
            this.gvSafeInventory.ShowFooter = false;
            safeInventoryRptQuery.IsGetAll = false;
            safeInventoryRptQuery.CurrentPage = currentPageIndex;
            this.SetSafeInventoryRptQueryEntity(safeInventoryRptQuery);
            base.Export2Excel("SafeInventoryRpt.xls", this.gvSafeInventory);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            safeInventoryRptQuery.CurrentPage = "0";
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            safeInventoryRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            safeInventoryRptQuery.OWNER_ID = "";
            safeInventoryRptQuery.SKU_CLASS_ID = "";
            safeInventoryRptQuery.SKU_ID = "";
            safeInventoryRptQuery.VENDOR_ID = "";
            safeInventoryRptQuery.SHOW_ALL = false;
            safeInventoryRptQuery.SHOW_VENDOR = false;
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            if (int.Parse(safeInventoryRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                safeInventoryRptQuery.CurrentPage = (int.Parse(safeInventoryRptQuery.CurrentPage) + 1).ToString();
                this.BindData2SafeInventoryList(safeInventoryRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            if (int.Parse(safeInventoryRptQuery.CurrentPage) > 0)
            {
                safeInventoryRptQuery.CurrentPage = (int.Parse(safeInventoryRptQuery.CurrentPage) - 1).ToString();
                this.BindData2SafeInventoryList(safeInventoryRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
        }

        private SafeInventoryRptQueryEntity GetSafeInventoryRptQueryEntity()
        {
            return (base.PageQueryEntitySession as SafeInventoryRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
                safeInventoryRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SafeInventoryList(safeInventoryRptQuery);
            }
        }

        protected void gvSafeInventory_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15271", this.gvSafeInventory);
            SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
            BoundField columnVendor = null;
            foreach (DataControlField columnField in this.gvSafeInventory.Columns)
            {
                if ((columnField is BoundField) && (((BoundField) columnField).DataField.ToUpper() == "VENDOR_ID"))
                {
                    columnVendor = (BoundField) columnField;
                    break;
                }
            }
            if (!this.PreviousPage.ShowVendor)
            {
                this.gvSafeInventory.Columns.Remove(columnVendor);
            }
        }

        protected void gvSafeInventory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal minQty = 0M;
                decimal maxQty = 0M;
                decimal invQty = 0M;
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblReorderPoint")) != null)
                {
                    this.TotalReorderPoint += decimal.Parse((((Label) e.Row.FindControl("lblReorderPoint")).Text != "") ? ((Label) e.Row.FindControl("lblReorderPoint")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblMinQty")) != null)
                {
                    minQty = decimal.Parse((((Label) e.Row.FindControl("lblMinQty")).Text != "") ? ((Label) e.Row.FindControl("lblMinQty")).Text : "0");
                    this.TotalMinQty += decimal.Parse((((Label) e.Row.FindControl("lblMinQty")).Text != "") ? ((Label) e.Row.FindControl("lblMinQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblMaxQty")) != null)
                {
                    maxQty = decimal.Parse((((Label) e.Row.FindControl("lblMaxQty")).Text != "") ? ((Label) e.Row.FindControl("lblMaxQty")).Text : "0");
                    this.TotalMaxQty += decimal.Parse((((Label) e.Row.FindControl("lblMaxQty")).Text != "") ? ((Label) e.Row.FindControl("lblMaxQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    invQty = decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                    this.TotalInvQtyText += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                    if ((minQty != 0M) && (invQty < minQty))
                    {
                        ((Image) e.Row.FindControl("imgPriority")).ImageUrl = "../Images/min.gif";
                    }
                    else if ((maxQty != 0M) && (invQty > maxQty))
                    {
                        ((Image) e.Row.FindControl("imgPriority")).ImageUrl = "../Images/max.gif";
                    }
                    else
                    {
                        ((Image) e.Row.FindControl("imgPriority")).ImageUrl = "../Images/OP05.gif";
                    }
                }
                if (((Label) e.Row.FindControl("lblPreallocatedQty")) != null)
                {
                    this.TotalPreallocatedQty += decimal.Parse((((Label) e.Row.FindControl("lblPreallocatedQty")).Text != "") ? ((Label) e.Row.FindControl("lblPreallocatedQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblAvailableQty")) != null)
                {
                    this.TotalAvailableQty += decimal.Parse((((Label) e.Row.FindControl("lblAvailableQty")).Text != "") ? ((Label) e.Row.FindControl("lblAvailableQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalReorderPointText.Text = base.GetPreferenceDigit(this.TotalReorderPoint);
                this.lblTotalMinQty.Text = base.GetPreferenceDigit(this.TotalMinQty);
                this.lblTotalMaxQty.Text = base.GetPreferenceDigit(this.TotalMaxQty);
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalInvQtyText);
                this.lblTotalPreallocatedQty.Text = base.GetPreferenceDigit(this.TotalPreallocatedQty);
                this.lblTotalAvailableQty.Text = base.GetPreferenceDigit(this.TotalAvailableQty);
            }
        }

        protected void gvSafeInventory_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                SafeInventoryRptQueryEntity safeInventoryRptQuery = this.GetSafeInventoryRptQueryEntity();
                safeInventoryRptQuery.SortField = e.SortExpression;
                if (safeInventoryRptQuery.SortDirection == "ASC")
                {
                    safeInventoryRptQuery.SortDirection = "DESC";
                }
                else if (safeInventoryRptQuery.SortDirection == "DESC")
                {
                    safeInventoryRptQuery.SortDirection = "ASC";
                }
                this.BindData2SafeInventoryList(safeInventoryRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            SafeInventoryRptQueryEntity safeInventoryRptQuery = new SafeInventoryRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    safeInventoryRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    safeInventoryRptQuery.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    safeInventoryRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    safeInventoryRptQuery.VENDOR_ID = this.PreviousPage.VendorID;
                    safeInventoryRptQuery.SHOW_ALL = this.PreviousPage.ShowAllInv;
                    safeInventoryRptQuery.SHOW_VENDOR = this.PreviousPage.ShowVendor;
                }
                else
                {
                    safeInventoryRptQuery.OWNER_ID = "";
                    safeInventoryRptQuery.SKU_CLASS_ID = "";
                    safeInventoryRptQuery.SKU_ID = "";
                    safeInventoryRptQuery.VENDOR_ID = "";
                    safeInventoryRptQuery.SHOW_ALL = false;
                    safeInventoryRptQuery.SHOW_VENDOR = false;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            safeInventoryRptQuery.IsGetAll = false;
            safeInventoryRptQuery.CurrentPage = "0";
            safeInventoryRptQuery.PageSize = this.gvSafeInventory.PageSize.ToString();
            safeInventoryRptQuery.SortField = "A.OWNER_ID";
            safeInventoryRptQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvSafeInventory.PageSize;
            this.SetSafeInventoryRptQueryEntity(safeInventoryRptQuery);
            this.BindData2SafeInventoryList(safeInventoryRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvSafeInventory);
                this.LoadData();
            }
        }

        private void SetSafeInventoryRptQueryEntity(SafeInventoryRptQueryEntity safeInventoryRptQuery)
        {
            base.PageQueryEntitySession = safeInventoryRptQuery;
        }

        public SafeInventoryRptQuery PreviousPage
        {
            get
            {
                return (SafeInventoryRptQuery) base.PreviousPage;
            }
        }
    }
}

